﻿using System;
using System.Data;
using System.Data.SqlClient;
using AccessControl.Common;


namespace AccessControl.DataAcess
{
	public class FingerCollectionAccess
	{
		public void Insert(AccessControlDS.FingerCollectionRow row)
		{
			using (var cn = new SqlConnection(Util.ConnectionString))
			{
				var cmdInsert = new SqlCommand("SP_FingerCollection_INS", cn) { CommandType = CommandType.StoredProcedure };
				cmdInsert.Parameters.AddWithValue("@StudentCode",row.StudentCode );
				cmdInsert.Parameters.AddWithValue("@GroupUserID", row.GroupUserID);
				cmdInsert.Parameters.AddWithValue("@DateRegistered", Util.GetServerDateTime());
				cmdInsert.Parameters.AddWithValue("@Finger11",row.Finger11);
				cmdInsert.Parameters.AddWithValue("@Finger12",row.Finger12);
				cmdInsert.Parameters.AddWithValue("@Finger13",row.Finger13);
				cmdInsert.Parameters.AddWithValue("@Finger21",row.Finger21);
				cmdInsert.Parameters.AddWithValue("@Finger22",row.Finger22);
				cmdInsert.Parameters.AddWithValue("@Finger23",row.Finger23);
				cn.Open();
				cmdInsert.ExecuteNonQuery();
				cn.Close();
			}
		}

		public int CountByStudent(String studentCode)
		{
			using (var cn = new SqlConnection(Util.ConnectionString))
			{
				var cmdSelectCount = new SqlCommand("SELECT COUNT(StudentCode) FROM FingerCollection WHERE StudentCode=@StudentCode ", cn);
				cmdSelectCount.Parameters.AddWithValue("@StudentCode", studentCode);
				cn.Open();
				var i = (int)cmdSelectCount.ExecuteScalar();
				cn.Close();
				return i;
			}
		}

		public void DeleteByStudentCode(String studentCode)
		{
			using (var cn = new SqlConnection(Util.ConnectionString))
			{
				var cmdInsert = new SqlCommand("SP_FingerCollection_DEL", cn) { CommandType = CommandType.StoredProcedure };
				cmdInsert.Parameters.AddWithValue("@StudentCode",studentCode);
				cn.Open();
				cmdInsert.ExecuteNonQuery();
				cn.Close();
			}
		}
        public void UpdateTemplate(String fkPerson,byte fingerIndex,int G,byte[] template)
        {
            using (var cn = new SqlConnection(Util.ConnectionString)){

                var cmd = new SqlCommand("UPDATE tblFingerTemplate SET Template=@Template,UpdateDate=@UpdateDate,TemplateSize=@TemplateSize,TemplateG=@TemplateG WHERE fkPerson=@fkPerson AND TemplateFinger=@TemplateFinger", cn);
                cmd.Parameters.Add("fkPerson", SqlDbType.VarChar, 20).Value = fkPerson;
                cmd.Parameters.Add("TemplateFinger", SqlDbType.TinyInt).Value = fingerIndex;
                cmd.Parameters.Add("Template", SqlDbType.VarBinary,template.Length).Value = template;
                cmd.Parameters.Add("UpdateDate", SqlDbType.DateTime).Value = DateTime.Now;
                cmd.Parameters.Add("TemplateSize", SqlDbType.Int).Value = template.Length;
                cmd.Parameters.Add("TemplateG", SqlDbType.Int).Value = G;
                
                cn.Open();
                cmd.ExecuteNonQuery();
                cn.Close();
            }
        }
    }
}
